package {
	import com.combiform.core.IO_Handler;
	import com.combiform.core.IR_Event;
	import com.combiform.core.Combine_Obj;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;

	public class myIO_Handler extends IO_Handler{
		//knob values
		//public var knobInitial:Array = [100,300,700,900];
		//public var knobs:Array = [100,300,700,900]; //must be same as above
		//public var knobLeft:Array = [0,0,0,0];
		//public var knobRight:Array = [0,0,0,0];
		//public var knobMax:Number = 1023;
		//public var knobSpeed:Number = 50;

		public function myIO_Handler(st:Stage, uk:Boolean=true, um:Boolean=false) {
			//this code creates the arduino handler which manages the connection to all of the controllers
			super(st, uk, um);
			using_mouse=um;
			using_keyboard=uk;
		}

		override public function frameH():void{
			//this line needs to be added to read input from the controllers
			a_h.frameH();
			
			/*
			//analyze knobs
			var i:int, knobDirection:int, changed:Boolean;
			for (i=0; i<knobs.length; ++i)
			{
				changed = false;
				knobDirection = knobRight[i] - knobLeft[i];
				
				if (knobs[i] > 0 && knobDirection < 0)
				{
					if (knobs[i] <= knobSpeed)
						knobs[i] = 0;
					else
						knobs[i] -= knobSpeed;
					changed = true;
				}
				else if (knobs[i] < knobMax && knobDirection > 0)
				{
					if (knobs[i] >= (knobMax-knobSpeed))
						knobs[i] = knobMax;
					else
						knobs[i] += knobSpeed;
					changed = true;
				}
				if (changed)
				{
					dispatchEvent(new IR_Event(IR_Event.INPUT,IR_Event.IR_ACTIONBUTTON, knobs[i], i));
				}
			}*/
		}

		override public function HandleKbdDown_Input(kevent:KeyboardEvent):void{
			trace ("myIO_H:HandleKbdDown_Input: keyCode="+kevent.keyCode)
			switch (kevent.keyCode) {
				//this is temporary fix to a known hardware issue
				//this command basically restarts the controller connection
				case 187: a_h.createACs(); trace "="; break; //=
				case 87: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,0)); break;//player 0 sends push button down W
				case 83: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,1)); break;//player 1 sends push button down S
				case 85: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,2)); break;//player 1 sends push button down U
				case 74: dispatchEvent(new IR_Event(IR_Event.INPUT, IR_Event.IR_ACTIONBUTTON2,IR_Event.IR_BUTTON_DOWN,3)); break;//player 1 sends push button down J
				/*
				case 81: knobLeft[0] = 1;	break; //Q
				case 69: knobRight[0] = 1;	break; //E
				case 65: knobLeft[1] = 1;	break; //A
				case 68: knobRight[1] = 1;	break; //D
				case 89: knobLeft[2] = 1;	break; //Y
				case 73: knobRight[2] = 1;	break; //I
				case 72: knobLeft[3] = 1;	break; //H
				case 75: knobRight[3] = 1;	break; //K
				*/
				
				default: break;
			}
		}

		override public function HandleKbdUp_Input(kevent:KeyboardEvent):void{
			switch (kevent.keyCode) {
				
				default: break;
			}
		}
		override public function HandleMouseClick(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseClick"+me.stageX+","+me.stageY);
		}
		override public function HandleMouseDoubleClick(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseDoubleClick"+me.stageX+","+me.stageY);
		}
		override public function HandleMouseMove(me:MouseEvent):void{
			if (debug) trace ( "HandleMouseDoubleClick"+me.stageX+","+me.stageY);
		}

		override public function HandleMC_Input( irevent:IR_Event):void{
			if (debug) irevent.print();
			dispatchEvent(irevent);
		}

	}
}
